package com.csr;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class SettingsActivity extends Activity
{
	ListView list;
	Button btnBack,btnLogout;
	TextView tveaderText;
	
	String[] values = new String[] 
	{
	   "Change Login Password","Change Transaction PIN"
	};  
	Integer[] images=new Integer[]
			{
				R.drawable.change_password,
				R.drawable.change_transaction_pin,
				
			};
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);
		
		 
		list=(ListView)findViewById(R.id.lvSettingsList);
		 	
		btnBack=(Button)findViewById(R.id.btnPrevious);
	    btnLogout=(Button)findViewById(R.id.btnLogout);
	        
	    tveaderText=(TextView)findViewById(R.id.tv_headerText);
	    tveaderText.setText("Settings");
	             
	        MyListAdapter adapter = new MyListAdapter(this, values,images);
	        list.setAdapter(adapter);
	        
	        list.setOnItemClickListener(new OnItemClickListener()
	        {

				public void onItemClick(AdapterView<?> adapterView, View view, int position,
						long id)
				{
					
					
					if(position==0)
					{
						  new ChangePinDialog(SettingsActivity.this).show();
					}
					else if(position ==1)
					{
						new ChangeTransactionPin(SettingsActivity.this).show();
						
					}
				}
	        	
			});
	        btnBack.setOnClickListener(new OnClickListener()
			{
				
				public void onClick(View arg0) 
				{
					// TODO Auto-generated method stub
					finish();
				}
				
			});
			
			btnLogout.setOnClickListener(new OnClickListener()
			{
				
				public void onClick(View v) 
				{
					new AlertDialog.Builder(SettingsActivity.this)
			        .setTitle( "Exit Application" )
			        .setMessage( "Are you sure you want to Logout?" )
			        .setPositiveButton("YES", new android.content.DialogInterface.OnClickListener() 
			        {
			            public void onClick(DialogInterface arg0, int arg1) 
			            {
			                //do stuff onclick of YES
			            	setResult(2);
			            	finish();
			            }

						public void onClick(View v) 
						{
							// TODO Auto-generated method stub
							
						}
			        })
			        .setNegativeButton("NO",  new android.content.DialogInterface.OnClickListener() {
			            public void onClick(DialogInterface arg0, int arg1)
			            {
			                //do stuff onclick of CANCEL
			            	arg0.dismiss();
			               
			            }

						public void onClick(View v) 
						{
							// TODO Auto-generated method stub
						}
			        }).show();
					  		
				}
			});
		
		
	}
	 public static class ViewHolder
	 {
			public TextView tvItemName;
			public ImageView ivItemImage;
			
			 
     }
	 class MyListAdapter extends ArrayAdapter<String> 
	 {
			
			private final Context context;
			private final String[] values;
			private final Integer[] images;
			LayoutInflater vi ;
		  
			public MyListAdapter(Context context, String[] values,Integer[] images)
			{
				super(context, R.layout.row_lv_settings,values);
				
				this.context = context;
				this.values = values; 
				this.images=images;
				vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			}
 
			public View getView(final int position, View convertView, ViewGroup parent) 
			{
				 View v = convertView;
				 ViewHolder holder = null;
				 
				if (v == null) 
				{
						holder=new ViewHolder();  
						v = vi.inflate(R.layout.row_lv_settings, null); 
						holder.tvItemName=(TextView) v.findViewById(R.id.tvItemName);
						holder.ivItemImage=(ImageView)v.findViewById(R.id.ivOperatorIcon);
						v.setTag(holder); 
					 
				}
				else
					holder=(ViewHolder)v.getTag();
					String name=this.values[position];
					System.out.println(name);
					holder.tvItemName.setText(name);
					holder.ivItemImage.setImageResource(this.images[position]);
					
				 	return v; 
			}
		}	
	 
}
